package com.houserent.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.houserent.dao.HouseDao;
import com.houserent.dao.LogDao;
import com.houserent.dao.MsgDao;
import com.houserent.entry.HouseInfo;
import com.houserent.entry.HouseResult;
import com.houserent.entry.LogInfo;
import com.houserent.entry.MsgInfo;
import com.houserent.utils.HouseUtils;

@Service
@Transactional
public class MsgServiceImpl implements MsgService {

    @Resource
    private MsgDao msgDao;
    @Resource
    private LogDao logDao;
    @Resource
    private HouseDao houseDao;

    @Override
    public HouseResult getAllMassageInfo(String userId) {
        LogInfo user = logDao.login(userId);
        Map sqlMap = HouseUtils.asMap("userId", userId);
        if ("01".equals(user.getUser_type())) {// 如果是房客
            HouseInfo house = houseDao.getHouseInfoByHouseUser(userId);
            sqlMap.put("hostId", house.getHost_id());
        }
        List<MsgInfo> msgs = msgDao.getMsgByUserId(sqlMap);
        if (HouseUtils.isEmpty(msgs)) {
            return new HouseResult(HouseResult.ERROR_CODE, "暂无消息", "");
        }
        return new HouseResult(HouseResult.SUCCESS_CODE, "查询成功", msgs);
    }

    @Override
    public HouseResult sendMsg(String authorId, String reciver, String msgType, String msgTatile, String msgContent) {
        // 发送消息,公告和报修
        LogInfo author = logDao.login(authorId);
        String[] recivers = null;
        if ("1,2".contains(msgType)) {// 公告
            recivers = new String[] { reciver };
        }
        if ("0".equals(msgType)) {// 消息
            if (reciver.endsWith(",")) {
                reciver = reciver.substring(0, reciver.length() - 1);
            }
            recivers = reciver.split(",");
            if (HouseUtils.isEmpty(recivers) && recivers.length == 0) {
                return new HouseResult(HouseResult.ERROR_CODE, "未获取到接收人,请重试", "");
            }
        }
        for (int i = 0; i < recivers.length; i++) {
            reciver = recivers[i];
            String msgId = HouseUtils.createMsgID();
            String readTag = "0";// 未读
            String sendDate = HouseUtils.getSysDate();
            MsgInfo msg = new MsgInfo(msgId, authorId, author.getReal_name(), author.getUser_type(), reciver, msgType,
                    msgTatile, readTag, msgContent, sendDate, null, null);
            msgDao.sendMsg(msg);
        }
        return new HouseResult(HouseResult.SUCCESS_CODE, "发送成功", "");
    }

    @Override
    public HouseResult modifyReadTag(String msgId, String userId) {
        if (HouseUtils.isEmpty(msgId)) {
            return new HouseResult(HouseResult.ERROR_CODE, "修改消息阅读标识失败", "");
        }
        MsgInfo msg = msgDao.getMsgInfoByMsgId(msgId);
        if (msg.getAuthor_id().equals(userId)) {
            return new HouseResult(HouseResult.ERROR_CODE, "作者无需修改未读消息状态", "");
        }
        msgDao.modifyReadTag(HouseUtils.asMap("readTag", "1", "msgId", msgId));
        return new HouseResult(HouseResult.SUCCESS_CODE, "修改消息阅读标识成功", "");
    }
}
